package com.unicampmaps;

import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.FilterQueryProvider;
import android.widget.FrameLayout;
import android.widget.GridView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.widget.SearchView;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity {

	public FragmentManager mFragmentManager;
	
	private final int PANEL_CANTINAS = 0;
	private final int PANEL_SALAS_DE_AULA = 1;
	private final int PANEL_SERVICOS = 2;
	private final int PANEL_BIBLIOTECAS = 3;
	private final int PANEL_TRANSPORTE = 4;
	private final int PANEL_OUTROS = 5;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.mFragmentManager = getSupportFragmentManager();

		ActionBar actionBar = this.getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setLogo(R.drawable.actionbar_logo);
        actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_background));

        SlidingMenu slidingMenu = getSlidingMenu();
        slidingMenu.setBehindWidthRes(R.dimen.sliding_menu_width);
        slidingMenu.setBehindScrollScale(0.0f);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        setBehindContentView(R.layout.fragment_menu);
        
        
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction().replace(R.id.frag_menu, new MenuFragment()).commit();

        		
		GridView grid = (GridView) findViewById(R.id.gridview);
 
		grid.setAdapter(new HomePanelAdapter(this));
		
		grid.setOnItemClickListener(new OnItemClickListener() {
	        @Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {	        
	        	Intent intent;
	        	switch (position) {
	        		case PANEL_CANTINAS:
	        			ListActivity.curr_type = VenueData.T_RESTAURANT;
	        			break;
	        		case PANEL_SALAS_DE_AULA:
	        			ListActivity.curr_type = VenueData.T_CLASSROOM;
	        			break;
	        		case PANEL_SERVICOS:
	        			ListActivity.curr_type = VenueData.T_SERVICES;
	        			break;
	        		case PANEL_BIBLIOTECAS:
	        			ListActivity.curr_type = VenueData.T_LIBRARY;
	        			break;
	        		case PANEL_TRANSPORTE:
	        			//ListActivity.curr_type = VenueData.T_TRANSPORT;
	        			List<Venue> venues = Venue.getVenues(MainActivity.this.getContentResolver(), VenueData.T_TRANSPORT);
	        			BasicMapViewer.emptyVenues();
	    				for (int i = 0; i < venues.size(); i++) {
	    					BasicMapViewer.addVenue(venues.get(i));
	    				}
	    				
	        			intent = new Intent(arg0.getContext(), BasicMapViewer.class);
	        	        startActivity(intent);
	        			return;
	        		case PANEL_OUTROS:
	        			ListActivity.curr_type = VenueData.T_OTHER;
	        			break;
	        	}
	        	intent = new Intent(arg0.getContext(), ListActivity.class);
	        	startActivity(intent);
	        }
	    });
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu_search, menu);
	    MenuItem searchViewItem = menu.findItem(R.id.menu_search);
	    SearchView searchView = (SearchView) searchViewItem.getActionView();
	    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(350, FrameLayout.LayoutParams.WRAP_CONTENT);
	    searchView.setLayoutParams(lp);

	    searchView.setIconifiedByDefault(false);
	    searchView.setSubmitButtonEnabled(false);
	    
	    AutoCompleteTextView txtView = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
	    
	    txtView.setTextColor(Color.BLACK);
	    
	    final int[] to = new int[]{android.R.id.text1};
        final String[] from = new String[]{VenueData.C_NAME};
        VenueCursorAdapter adapter = new VenueCursorAdapter(this, R.layout.autocomplete_item, null, from, to, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        // This will provide the labels for the choices to be displayed in the AutoCompleteTextView
        adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
            @Override
            public CharSequence convertToString(Cursor cursor) {
                final int colIndex = cursor.getColumnIndexOrThrow(VenueData.C_NAME);
                return cursor.getString(colIndex);
            }
        });
        
        adapter.setFilterQueryProvider(new FilterQueryProvider() {
            @Override
            public Cursor runQuery(CharSequence description) {
            	if (description == null) {
            		description = "";
            	}
            	String[] projection = new String[]{VenueData.C_ID, VenueData.C_NAME, VenueData.C_ABBR, VenueData.C_IMAGE};
            	String selection = VenueData.C_NAME + " LIKE ? OR " + VenueData.C_ABBR + " LIKE ?";
            	String[] selectionArgs = new String[]{"%" + description.toString() + "%", "%" + description.toString() + "%"};
            	Cursor managedCursor = MainActivity.this.getContentResolver().query(VenueProvider.CONTENT_URI, projection, selection, selectionArgs, null);
            	System.out.println(managedCursor.getCount() + " rows");
                return managedCursor;
            }
        });
        
		txtView.setAdapter(adapter);
		
		txtView.setOnItemClickListener(new OnItemClickListener() { 

		    @Override
		    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
		    	DetailsActivity.curr_venue_id = id;
		        Intent intent = new Intent(arg0.getContext(), DetailsActivity.class);
		        startActivity(intent); 
		    }
		});
		
	    
	    return true;
	}
}
